package com.springsecurity.demo.config;///* Copyright (c) 2022, TD SYNNEX Corporation. All rights reserved */

import io.swagger.annotations.Api;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * http://localhost:8082/spring-security-demo/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
	@Bean
	public Docket swaggerDocket(ApplicationContext context) {
		return new Docket(DocumentationType.SWAGGER_2)
				.select()
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.paths(PathSelectors.any())
				.build()
				.apiInfo(apiInfo())
//				.securitySchemes(securitySchemes())
//				.securityContexts(securityContexts())
				;

	}

//	private List<SecurityScheme> securitySchemes() {
//		//设置请求头信息
//		List<SecurityScheme> result = new ArrayList<>();
//		ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
//		result.add(apiKey);
//		return result;
//	}
//
//	private List<SecurityContext> securityContexts() {
//		//设置需要登录认证的路径
//		List<SecurityContext> result = new ArrayList<>();
////		result.add(getContextByPath("/brand/.*"));
//		return result;
//	}
//
//	private SecurityContext getContextByPath(String pathRegex) {
//		return SecurityContext.builder()
//				.securityReferences(defaultAuth())
//				.forPaths(PathSelectors.regex(pathRegex))
//				.build();
//	}
//
//	private List<SecurityReference> defaultAuth() {
//		List<SecurityReference> result = new ArrayList<>();
//		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//		authorizationScopes[0] = authorizationScope;
//		result.add(new SecurityReference("Authorization", authorizationScopes));
//		return result;
//	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("Spring Security RESTful APIs")
				.description("Spring Security  RESTful APIs")
				.version("1.0.0")
				.build();
	}
}
